<?php
namespace Admin\Controller;
use Think\Controller;
class RuleController extends CommonController {
    public function lists(){

        $count = $this->Model->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','首页');
        $Page->setConfig('last','最后一页');
        $show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        $list = $this->Model->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出


        $this->display();

    }

    /**
     * 权限添加
     *
     * @return void
     * @author
     **/
    function add()
    {
        if(IS_POST){
            $id = $this->Model->add(I('post.'));
            echo $this->Model->getLastSql();die;
            if($id){
                $this->success('权限添加成功',U('Admin/rule/lists'),1);die;
            }else{
                $this->error('权限添加失败','',1);die;
            }
        }

        $this->display();
    }


    /*权限数据导出方法*/
    function to_out(){

        $list = $this->Model->select();

        // 引入excel第三方类库
        Vendor('PHPExcel.PHPExcel');

        $phpexcel = new \PHPExcel;

             $phpexcel->getActiveSheet()->setCellValue('A1','名称');
             $phpexcel->getActiveSheet()->setCellValue('B1','规则');
             $phpexcel->getActiveSheet()->setCellValue('C1','类型');
             $phpexcel->getActiveSheet()->setCellValue('D1','状态');
             $phpexcel->getActiveSheet()->setCellValue('E1','所属父级');
             $phpexcel->getActiveSheet()->setCellValue('F1','前/后台');

        foreach ($list as $key => $v) {
            $key +=2;
            $phpexcel->getActiveSheet()->setCellValue('A'.$key,$v['name']);
            $phpexcel->getActiveSheet()->setCellValue('B'.$key,$v['title']);
            $phpexcel->getActiveSheet()->setCellValue('C'.$key,$v['type']);
            $phpexcel->getActiveSheet()->setCellValue('D'.$key,$v['status']);
            $phpexcel->getActiveSheet()->setCellValue('E'.$key,$v['pid']);
            $phpexcel->getActiveSheet()->setCellValue('F'.$key,$v['is_home']);
        }

        $phpexcel->createSheet();// 创建excel节点

        // 创建Excel文件类型2007
        $write = \PHPExcel_IOFactory::createWriter($phpexcel,'Excel2007');
        // print_r($write);die;

        header('Content-Type: application/vnd.ms-excel; charset=utf-8');
        header('Content-Disposition: attachment;filename=权限列表.xlsx');
        header('Cache-Control: max-age=0');

        $write->save('php://output');
    }
}